package zero.tech.games.dn.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.KeyVal;
import zero.tech.core.util.RandomUtil;
import zero.tech.games.common.BigCalSource;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.playback.Playback;
import zero.tech.games.common.playback.PlaybackBasic;
import zero.tech.games.dn.data.DnBigPanel;
import zero.tech.games.dn.data.DnConfig;
import zero.tech.games.dn.data.DnGameBase;
import zero.tech.games.dn.data.DnSmallPanel;
import zero.tech.games.dn.data.entity.PlayerData;
import zero.tech.games.dn.data.entity.PlayerSmallCal;
import zero.tech.games.dn.data.playback.DnPlaybackBigOverStep;
import zero.tech.games.dn.data.playback.DnPlaybackFapaiStep;
import zero.tech.games.dn.data.playback.DnPlaybackOperateStep;
import zero.tech.games.dn.data.playback.DnPlaybackSmallOverStep;
import zero.tech.games.entity.games.Club;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.common.RoomCache;
import zero.tech.games.logic.data.res.ResSeat;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.redis.queue.QueueUtil;
import zero.tech.wsnet.controller.LogicController;
public class PlaybackUtil {
	public static void saveClubBigPlayback(Room room, int source) {
		Club club=room.getClub();
		if(club!=null) {
			RoomCache roomCache = room.getRoomCache();
			if(source==BigCalSource.SMALLOVER_BIG_CAL) {
				int club_id=club.getClub_id();
				int game_id=room.getGame_id();
				String room_id=room.getRoom_id();
				String config_str=room.getConfigStr();
				String play_code=RandomUtil.randNum(8);
				int room_times=room.getRoom_times();
				Playback playback=new Playback();
				playback.setClub_id(club_id);
				playback.setGame_id(game_id);
				playback.setRoom_id(room_id);
				playback.setRoom_times(room_times);
				playback.setPlay_code(play_code);
				playback.setConfig_str(config_str);
				playback.setFapai_step(packageFapaiStep(room));
				playback.setOperate_step(packageOperateStep(room));
				playback.setSmallover_step(packageSmallOverStep(room));
				playback.setBigover_step(packageBigOverStep(room));
				playback.setCreate_stamp(new Date());
				playback.getUids().addAll(room.getControllersWithoutWather().keySet());
				DnSmallPanel dnSmallPanel = (DnSmallPanel) roomCache.getSmallPanels().get(room_times);
				List<PlayerSmallCal> list=dnSmallPanel.getPlayerCals();
				for(PlayerSmallCal playerSmallCal:list) {
					playback.getBasics().add(packagePlaybackBasic(playerSmallCal));
				}
				roomCache.getPlaybacks().add(playback);
			}
			QueueUtil queueUtil=ZeroContext.getInstance(QueueUtil.class);
			queueUtil.putQueueBatch(QueueConsts.QUEUE_PLAYBACK,roomCache.getPlaybacks());
		}
	}
	private static PlaybackBasic packagePlaybackBasic(PlayerSmallCal playerSmallCal) {
		PlaybackBasic playbackBasic= new PlaybackBasic();
		playbackBasic.setSimpleUser(playerSmallCal.getSimpleUser());
		playbackBasic.setCur_win_gold(playerSmallCal.getCur_win_gold());
		playbackBasic.setCur_win_score(playerSmallCal.getCur_win_score());
		return playbackBasic;
	}
	public static void saveClubSmallPlayback(Room room) {
		Club club=room.getClub();
		if(club!=null) {
			RoomCache roomCache = room.getRoomCache();
			int club_id=club.getClub_id();
			int game_id=room.getGame_id();
			String room_id=room.getRoom_id();
			int room_times=room.getRoom_times();
			String config_str=room.getConfigStr();
			String play_code=RandomUtil.randNum(8);
			Playback playback=new Playback();
			playback.setGame_id(game_id);
			playback.setClub_id(club_id);
			playback.setRoom_id(room_id);
			playback.setRoom_times(room_times);
			playback.setPlay_code(play_code);
			playback.setConfig_str(config_str);
			playback.setFapai_step(packageFapaiStep(room));
			playback.setOperate_step(packageOperateStep(room));
			playback.setSmallover_step(packageSmallOverStep(room));
			playback.setCreate_stamp(new Date());
			playback.getUids().addAll(room.getControllersWithoutWather().keySet());
			DnSmallPanel dnSmallPanel = (DnSmallPanel) roomCache.getSmallPanels().get(room_times);
			List<PlayerSmallCal> list=dnSmallPanel.getPlayerCals();
			for(PlayerSmallCal playerSmallCal:list) {
				playback.getBasics().add(packagePlaybackBasic(playerSmallCal));
			}
			roomCache.getPlaybacks().add(playback);
		}
	}

	private static JSONObject packageBigOverStep(Room room) {
		DnPlaybackBigOverStep dnPlaybackBigOverStep = new DnPlaybackBigOverStep();
		RoomCache roomCache = room.getRoomCache();
		DnBigPanel dnBigPanel=(DnBigPanel) roomCache.getBigPanel();
		dnPlaybackBigOverStep.setDnBigPanel(dnBigPanel);
		return (JSONObject) JSONObject.toJSON(dnPlaybackBigOverStep);
	}
	private static JSONObject packageSmallOverStep(Room room) {
		DnPlaybackSmallOverStep dnPlaybackSmallOverStep = new DnPlaybackSmallOverStep();
		RoomCache roomCache = room.getRoomCache();
		int room_times=room.getRoom_times();
		DnSmallPanel dnSmallPanel = (DnSmallPanel) roomCache.getSmallPanels().get(room_times);
		dnPlaybackSmallOverStep.setDnSmallPanel(dnSmallPanel);
		return (JSONObject) JSONObject.toJSON(dnPlaybackSmallOverStep);
	}

	private static JSONObject packageOperateStep(Room room) {
		DnPlaybackOperateStep dnPlaybackOperateStep = new DnPlaybackOperateStep();
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		DnConfig dnConfig = (DnConfig) room.getGameConfig();
		int banker_uid=dnGameBase.getBanker_uid();
		
		List<KeyVal<Integer,Integer>> qiangzhuanglist=new ArrayList<>();
		List<KeyVal<Integer,Integer>> yazhulist=new ArrayList<>();
		Map<Integer,PlayerData> playerDatas=dnGameBase.getPlayers();
		for(PlayerData playerData:playerDatas.values()) {
			int uid=playerData.getUid();
			if(dnConfig.getZhuang_mode()==0) {
				//抢庄
				if(playerData.getBuqiang()) {
					qiangzhuanglist.add(new KeyVal<>(uid,0));
				}else {
					qiangzhuanglist.add(new KeyVal<Integer, Integer>(uid, playerData.getQiangzhuang_multiple()));
				}
			}
			if(uid!=banker_uid) {
				//压注
				yazhulist.add(new KeyVal<Integer, Integer>(uid, playerData.getYazhu_multiple()));
			}
		}
		dnPlaybackOperateStep.getQiangzhuanglist().addAll(qiangzhuanglist);
		dnPlaybackOperateStep.getYazhulist().addAll(yazhulist);
		return (JSONObject) JSONObject.toJSON(dnPlaybackOperateStep);
	}

	private static JSONObject packageFapaiStep(Room room) {
		DnPlaybackFapaiStep dnPlaybackFapaiStep = new DnPlaybackFapaiStep();
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		dnPlaybackFapaiStep.setBanker_uid(dnGameBase.getBanker_uid());
		Map<Integer,Integer> map =new HashMap<Integer, Integer>();
		map.putAll(room.getSeats());
		for(Entry<Integer,Integer> entry:map.entrySet()) {
			Integer pos=entry.getKey();
			Integer uid=entry.getValue();
			if(uid!=null) {
				LogicController logicController=room.getAllControllers().get(uid);
				ResSeat seat=PckUtil.packageResSeat(logicController);
				dnPlaybackFapaiStep.getRes_seats().put(pos,seat);
			}
		}
		Map<Integer,PlayerData> playerDatas=dnGameBase.getPlayers();
		for(PlayerData playerData:playerDatas.values()) {
			int uid=playerData.getUid();
			dnPlaybackFapaiStep.getDnoutmap().put(uid, playerData.getDnOut());
		}
		return (JSONObject) JSONObject.toJSON(dnPlaybackFapaiStep);
	}

}
